#ifndef PARSEEIT_H
#define PARSEEIT_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define EIT_EVENT_MAX 128

#define EIT_PF_TABLE_ID 0x4e
#define EIT_SE_TABLE_ID 0x50
#define EIT__DESCRIPTOR_MAX 4096
typedef struct TS_EIT_EVENT
{
    unsigned int event_id :16;
    unsigned char start_time[5];
    unsigned int duration :24;
    unsigned int running_status :3;
    unsigned int free_CA_mode :1;
    unsigned int descriptors_loop_length :12;
    unsigned char descriptor[EIT__DESCRIPTOR_MAX];
} TS_EIT_EVENT;

typedef struct TS_EIT
{
    unsigned int table_id :8;
    unsigned int section_syntax_indicator :1;
    unsigned int reserved_future_use_1 :1;
    unsigned int reserved_1 :2;
    unsigned int section_length :12;
    unsigned int service_id :16;
    unsigned int reserved_2 :2;
    unsigned int version_number :5;
    unsigned int current_next_indicator :1;
    unsigned int section_number :8;
    unsigned int last_section_number :8;
    unsigned int transport_stream_id :16;
    unsigned int original_network_id :16;
    unsigned int segment_last_section_number :8;
    unsigned int last_table_id :8;

    TS_EIT_EVENT astEIT_Event[EIT_EVENT_MAX];

    unsigned int CRC_32 :32;
} TS_EIT;

class ParseEIT
{
public:
    ~ParseEIT();
    ParseEIT(const ParseEIT&)=delete;
    ParseEIT& operator=(const ParseEIT&)=delete;
    static ParseEIT& get_instance();

    /*********************************************************************************************************************************
     * Function Name : ParseEIT_Table
     * Description   :  parse the PAT table and get the information
     * Parameters    :
     * 				pfTsFile -- the TS file descriptor
     * 				iTsPosition -- the TS package begin position
     *				iTsLength -- the TS package length
     *				astProgram -- the program structure where we put the information in
     * Returns       : -1 failure 0 -- no the section in the transport stream 1 - all section over
     *********************************************************************************************************************************/
    int ParseEIT_Table(FILE *pfTsFile, int iTsPosition, int iTsLength, unsigned int uiTableId, unsigned int uiTableIdExtension);
private:
    /*********************************************************************************************************************************
     * Function Name : ParseEIT_SectionHead
     * Description   :  parse the EIT section head
     * Parameters    :
     * 				pstTS_EIT -- the TS_EIT
     *				pucSectionBuffer -- the SDT section buffer
     * Returns       : void
     *********************************************************************************************************************************/
    void ParseEIT_SectionHead(TS_EIT *pstTS_EIT, unsigned char *pucSectionBuffer);
    /*********************************************************************************************************************************
     * Function Name : ParseEIT_Section
     * Description   :  parse the EIT section
     * Parameters    :
     * 				pstTS_EIT -- the TS_EIT
     *				pucSectionBuffer -- the EIT section buffer
     * Returns       : the TS_EIT_EVENT count
     *********************************************************************************************************************************/
    int ParseEIT_Section(TS_EIT *pstTS_EIT, unsigned char *pucSectionBuffer);

    /*********************************************************************************************************************************
     * Function Name : PrintTheStartTime
     * Description   : out put the start time
     * Parameters    :
     *				pucStartTime -- the buffer of time
     * Returns       : void
     *********************************************************************************************************************************/
    void PrintTheStartTime(unsigned char *pucStartTime);
    /*********************************************************************************************************************************
     * Function Name : PrintTheDurationTime
     * Description   : out put the duration time
     * Parameters    :
     *				uiDuration -- the duration time
     * Returns       : void
     *********************************************************************************************************************************/
    void PrintTheDurationTime(unsigned int uiDurationTime);
    /*********************************************************************************************************************************
     * Function Name : PrintShortEventDescriptor
     * Description   : out put the event information in the descriptor tag 0x4d
     * Parameters    :
     *				pucDescriptor -- the descriptor
     *				uiDescriprotLength -- the descriptor length
     * Returns       : void
     *********************************************************************************************************************************/
    void PrintShortEventDescriptor(unsigned char *pucDescriptor, unsigned int uiDescriprotLength);
    /*********************************************************************************************************************************
     * Function Name : PrintEIT_Info
     * Description   : pint the EIT information
     * Parameters    :
     *				pstTS_EIT -- the TS_EIT
     * Returns       : void
     *********************************************************************************************************************************/
    void PrintEIT_Info(TS_EIT *pstTS_EIT, int iEventCount);
private:
    ParseEIT();
};

#endif // PARSEEIT_H
